package com.woniuxy.controller.dailycontroller;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;
import com.woniuxy.pojo.Accident;
import com.woniuxy.service.basics.BookService;
import com.woniuxy.service.basics.CarService;
import com.woniuxy.service.basics.DriverService;
import com.woniuxy.service.daily.AccidentService;

@Controller
@RequestMapping("accident")
public class AccidentController {
	@Autowired
	private AccidentService accidentService;
	@Autowired
	private BookService bookService;
	@Autowired
	private DriverService driverService;
	@Autowired
	private CarService carService;
	@RequestMapping("/getAll")
	public String getAll(Model model,HttpSession session) {
		System.out.println("-----------------------------------------------------");
		PageInfo pageInfo = accidentService.selectAll(1,"","");
		session.setAttribute("dr_pn", 1);
		session.setAttribute("dr_conditionBy", "");
		session.setAttribute("dr_condition", "");
		model.addAttribute("pageInfo", pageInfo);
		return "/daily/accident.jsp";
	}
	@RequestMapping("/getByCondition")
	public String getByCondition(Model model,HttpSession session,Integer dr_pn,String dr_conditionBy,String dr_condition) {
		Integer pn = Integer.parseInt(session.getAttribute("dr_pn").toString());
		String conditionBy = session.getAttribute("dr_conditionBy").toString();
		String condition = session.getAttribute("dr_condition").toString();
		System.out.println(pn);
		System.out.println(pn);
		System.out.println(conditionBy);
		System.out.println(condition);
		if (dr_pn!=null) {
			pn=dr_pn;
		}
		if (dr_conditionBy!=null) {
			conditionBy=dr_conditionBy;
		}
		if (dr_condition!=null) {
			condition=dr_condition;
			pn=1;
		}
		PageInfo pageInfo = accidentService.selectAll(pn,conditionBy,condition);
		session.setAttribute("dr_pn", pn);
		session.setAttribute("dr_conditionBy", conditionBy);
		session.setAttribute("dr_condition", condition);
		model.addAttribute("pageInfo", pageInfo);
		return "/daily/accident.jsp";
	}
	@RequestMapping("/add")
	public String add(Model model) {
		List<String> brCarlicense = carService.selectAllCarlicense();
		List<String> brdriver = driverService.selectAllDriverName();
		HashMap<String,List<String>> map = new HashMap<String, List<String>>();
		map.put("deptList", brdriver);
		map.put("deptLists", brCarlicense);
		model.addAllAttributes(map);
		return "/daily/accidentadd.jsp";
	}
	@RequestMapping("/doadd")
	public String doadd(Accident accident) {
		accidentService.addAccident(accident);
		return "/accident/getByCondition";
	}
	@RequestMapping("/deleteById")
	public String deleteById(Integer accidentId) {
		accidentService.deleteByAccidentId(accidentId);
		return "/accident/getByCondition";
	}
	@RequestMapping("/batchDelete")
	public String batchDelete(Integer[] cb,Model model) {
		int deleterows = accidentService.batchDelete(cb);
		model.addAttribute("deleterows", deleterows);
		return "/accident/getByCondition";
	}
	@RequestMapping("/showInfo")
	public String showInfo(Integer accidentId,Model model) {
		Accident accident = accidentService.selectById(accidentId);
		model.addAttribute("accident", accident);
		return "/daily/accidentinfo.jsp";
	}
	@RequestMapping("/update")
	public String update(ModelMap modelmap,Integer accidentId) {
		Accident accident = accidentService.selectById(accidentId);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		List<String> brdriver = driverService.selectAllDriverName();
		modelmap.put("deptList", brdriver);
		modelmap.put("accident", accident);
		return "/daily/accidentupdate.jsp";
	}
	@RequestMapping("/doUpdate")
	public String doUpdate(Accident accident) {
		accidentService.updateAccident(accident);
		return "/accident/getByCondition";
	}
}
